package com.tianhua.javs.core.parse;

import com.tianhua.javs.core.constant.TagConstants;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:对一行javs脚本进行进行展开形成多行类似于java风格的脚本
 * 类似于多项式扩展
 * 基于 -> 和 ->{
 * 进行换行和代码块构建
 *
 * date: 2022/2/14
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class SegmentSpreadParseService {


    /**
     * 按特定字符进行多项式展开
     * @param segment
     * @return
     */
    public  String [] spreadMult(String segment){
        List<String> segmentList = new ArrayList<>();
        if(segment.contains("->{")){
            if(segment.contains("sfore")){
                segment = segment.replace("}","\n});\n");
            }
            else if(segment.contains("fore(") || segment.contains("for(")){
                segment = segment.replace("->{","{\n");
                segment = segment.replace("}","\n}");
            }

            //按 ->{ 进行多项式展开
            String [] segmentArr = segment.split("->\\{");
            for (int i = 0; i < segmentArr.length; i++){

                String currentSeg = segmentArr[i];
                //存在循环关键字
                if(currentSeg.contains("sfore(")){
                    int index = currentSeg.indexOf("sfore(");
                    currentSeg = currentSeg.substring(0,index) + currentSeg.substring(index,currentSeg.length() -1 ).replaceFirst("\\)"," ");
                    segmentList.add(currentSeg);
                }else {
                    segmentList.add(segmentArr[i]);
                }
                if(i == segmentArr.length - 1){
                    continue;
                }
                segmentList.add("->{\n");
            }
        }
        StringBuilder spreadBuilder = new StringBuilder();

        if(CollectionUtils.isNotEmpty(segmentList)){
            for (int i = 0;i < segmentList.size(); i++){
                //按 -> 进行多项式展开
                if(segmentList.get(i).contains("->")){
                    String [] segmentArr = segmentList.get(i).split("->");
                    //根据分割的情况需要加多少}才能与{匹配的上
                    int addCount = segmentArr.length - 1;

                    for (int j = 0; j < segmentArr.length; j++){
                        spreadBuilder.append(segmentArr[j]);
                        if(j == segmentArr.length - 1){
                            continue;
                        }
                        spreadBuilder.append("{\n");
                        //对后面的元素中如果存在} 则新加一个与上面加的香对应
                        for (int k = j + 1 ; k < segmentArr.length; k++){
                            if(segmentArr[k].contains("}")){
                                segmentArr[k] = segmentArr[k].replaceFirst("}","}\n}");
                                addCount--;
                                break;
                            }
                        }

                    }
                    //说明segmentArr内部只匹配了一部分需要在segmentList后续的元素中找到存在}的元素
                    if(addCount > 0){
                        //对后面的元素中如果存在} 则新加一个与上面加的相对应
                        for (int k = i + 1 ; k < segmentList.size(); k++){
                            //处理匿名方法函数的块});
                            if(segmentList.get(k).contains("});")){
                                String newSeg = segmentList.get(k).replaceFirst("\\);",");\n}");
                                segmentList.set(k,newSeg);
                                break;
                            }
                            else if(segmentList.get(k).contains("}")){
                                String newSeg = segmentList.get(k).replaceFirst("}","}\n}");
                                segmentList.set(k,newSeg);
                                break;
                            }
                        }
                    }
                }else {
                    spreadBuilder.append(segmentList.get(i));
                }

            }
        }
        //按 -> 进行多项式展开
        else if(segment.contains("->")){
            //按 -> 进行多项式展开
            String [] segmentArr = segment.split("->");
            int addCount = segmentArr.length - 1;
            for (int i = 0;i < segmentArr.length;i ++){
                spreadBuilder.append(segmentArr[i]);
                if(i == segmentArr.length - 1){
                    continue;
                }
                spreadBuilder.append("{\n");

                //对后面的元素中如果存在} 则新加一个与上面加的香对应
                for (int j = i + 1 ; j < segmentArr.length; j++){
                    if(segmentArr[j].contains("}")){
                        segmentArr[j] = segmentArr[j].replaceFirst("}","}\n}");
                        addCount --;
                        break;
                    }
                }
            }
            //说明还欠缺一些}
            if(addCount > 0){
                for (int i = 0;i < addCount;i ++){
                    spreadBuilder.append("}\n");
                }
            }
        }

        return buildBlankPrefix(spreadBuilder.toString());
    }

    /**
     * 构建代码段空格前缀
     * @param rowSegment
     * @return
     */
    private String[] buildBlankPrefix(String rowSegment ){
        String [] rowSegmentArr = rowSegment.split("\n");
        String blank = "    ";
        int blankCount = 1;
        for (int i = 0;i < rowSegmentArr.length;i++){
            if(i == 0){
                blank = getBlankPrefix(rowSegmentArr[i]);
            }
            else {
                if(rowSegmentArr[i].endsWith("{")){
                    ++ blankCount;
                    rowSegmentArr[i] = rowSegmentArr[i].trim();
                    for (int j = 0;j < blankCount;j ++){
                        rowSegmentArr[i] = blank + rowSegmentArr[i];
                    }
                }
                else if(rowSegmentArr[i].endsWith("}") || rowSegmentArr[i].startsWith("}")){
                    -- blankCount;
                    rowSegmentArr[i] = rowSegmentArr[i].trim();
                    for (int j = 0;j < blankCount;j ++){
                        rowSegmentArr[i] = blank + rowSegmentArr[i];
                    }
                }
                else{
                    if(rowSegmentArr[i - 1].endsWith("{")){
                        ++ blankCount;
                        rowSegmentArr[i] = rowSegmentArr[i].trim();
                        for (int j = 0;j < blankCount;j ++){
                            rowSegmentArr[i] = blank + rowSegmentArr[i];
                        }
                    }
                }
            }
        }

        return rowSegmentArr;
    }

    /**
     * 获取javs方法脚本内容的第一行的开头缩进部分
     * @param shellStr
     * @return
     */
    public   String getBlankPrefix(String shellStr){
        char [] arr = shellStr.toCharArray();
        StringBuilder blankBuilder = new StringBuilder();
        for (char a : arr){
            if(a == ' '){
                blankBuilder.append(" ");
            }else {
                break;
            }
        }
        if(blankBuilder.toString().length() < TagConstants.BLANK.length()){
            return TagConstants.BLANK;
        }
        return blankBuilder.toString();
    }



}
